package com.ruoyi.nuclein.service.impl;

import com.ruoyi.nuclein.Feign.BasicDataFeign;
import com.ruoyi.nuclein.domain.Mission;
import com.ruoyi.nuclein.domain.MissionOvertime;
import com.ruoyi.nuclein.domain.Residentmission;
import com.ruoyi.nuclein.mapper.MissionOvertimeMapper;
import com.ruoyi.nuclein.service.IMissionOvertimeService;
import com.ruoyi.nuclein.service.IMissionService;
import com.ruoyi.nuclein.service.IResidentmissionService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.stream.Collectors;

/**
 * 核酸任务超时Service业务层处理
 * 
 * @author ruoyi
 * @date 2023-04-14
 */
@Service
public class MissionOvertimeServiceImpl implements IMissionOvertimeService{
    @Autowired
    private MissionOvertimeMapper missionOvertimeMapper;

    @Autowired
    private IResidentmissionService residentmissionService;

    @Autowired
    private IMissionService missionService;

    @Autowired
    private BasicDataFeign basicDataFeign;

    /**
     * 查询核酸任务超时
     * 
     * @param id 核酸任务超时主键
     * @return 核酸任务超时
     */
    @Override
    public MissionOvertime selectMissionOvertimeById(Long id){
        return missionOvertimeMapper.selectMissionOvertimeById(id);
    }

    /**
     * 查询核酸任务超时列表
     * 
     * @param missionOvertime 核酸任务超时
     * @return 核酸任务超时
     */
    @Override
    public List<MissionOvertime> selectMissionOvertimeList(MissionOvertime missionOvertime)
    {
        return missionOvertimeMapper.selectMissionOvertimeList(missionOvertime);
    }

    /**
     * 新增核酸任务超时
     * 
     * @param missionOvertime 核酸任务超时
     * @return 结果
     */
    @Override
    public int insertMissionOvertime(MissionOvertime missionOvertime)
    {
        return missionOvertimeMapper.insertMissionOvertime(missionOvertime);
    }

    /**
     * 修改核酸任务超时
     * 
     * @param missionOvertime 核酸任务超时
     * @return 结果
     */
    @Override
    public int updateMissionOvertime(MissionOvertime missionOvertime)
    {
        return missionOvertimeMapper.updateMissionOvertime(missionOvertime);
    }

    /**
     * 批量删除核酸任务超时
     * 
     * @param ids 需要删除的核酸任务超时主键
     * @return 结果
     */
    @Override
    public int deleteMissionOvertimeByIds(Long[] ids)
    {
        return missionOvertimeMapper.deleteMissionOvertimeByIds(ids);
    }

    /**
     * 删除核酸任务超时信息
     * 
     * @param id 核酸任务超时主键
     * @return 结果
     */
    @Override
    public int deleteMissionOvertimeById(Long id)
    {
        return missionOvertimeMapper.deleteMissionOvertimeById(id);
    }


    /**
     *  核酸超时处理
     * @return
     */
    @Override
    public int overtime() {
        // 先获取楼栋核酸的结束时间，是否小于当前时间，如果是则取出核酸的id
        List<Long> ids = missionService.selectMissionListByTime();
        // 条件判断
        if (ids == null) return 0;

        // 如果任务完成，修改楼栋状态
        List<Mission> missionList = ids.stream().map(id -> missionService.selectMissionById(id)).collect(Collectors.toList());
        List<Long> buildingIds = missionList.stream().map(Mission::getBuildingId).collect(Collectors.toList());
        basicDataFeign.taskChangeState(buildingIds);

        // 再根据每个任务id获取用户核酸的状态，如果是未完成 即等于1 则取出
        List<Residentmission> residentList = residentmissionService.selectResidentByIds(ids);
        // 将取出的用户添加到核酸超时表中(应该先判断，不想判断了 所以这里省略判断重复的步骤)
        for (Residentmission resident : residentList) {
            MissionOvertime missionOvertime = new MissionOvertime();
            missionOvertime.setVillageId(resident.getVillageId());
            missionOvertime.setBuildId(resident.getBuildingId());
            missionOvertime.setMissionId(resident.getNucleinMissionId());
            missionOvertime.setState(resident.getState().toString());
            missionOvertime.setStartTime(resident.getStartTime());
            missionOvertime.setEndTime(resident.getEndTime());
            missionOvertime.setResidentId(resident.getResidentId());
            missionOvertimeMapper.insertMissionOvertime(missionOvertime);
        }
        return 1;
    }

}
